'use strict';
// Declare app level module which depends on filters, and services
define([
        'route-config',
        'controllers/index',
        'controllers/space/space',
        'controllers/tools/tools',
        'controllers/games/games',
        'common/directives',
        'common/filters',
        'common/services'
],function(routeConfig,index,space,tools,games){
	var app =  angular.module('myApp', ['ui.router','ngCookies','CommDirectives', 'CommFilters', 'CommServices']);
		
		app.config(['$stateProvider', '$urlRouterProvider','$compileProvider', '$controllerProvider', 
		            function($stateProvider, $urlRouterProvider,$compileProvider, $controllerProvider){
			
			/***
			 * routeConfig.state({} , []);
			 * 第一个参数 :{routeName:'父级状态', controller:'父状态对应的ctrl',  path: '模板文件存储路径'}
			 * 			1)path不写时,默认存储路径为: routeName
			 * 第二个参数 : 数组类型, 子级状态 {name:'子状态', controller'子状态对应的ctrl文件', templateUrl:'模板名称', mName:'对应ctrl的名称'}
			 * 			1) mName不写时默认 ctrl名字为:name
			 * 			2) templateUrl不写时模式模板名称:name.html 
			 * 			3) url 默认是 name
			 */
			routeConfig.setCompileProvider($compileProvider);
	        routeConfig.setControllerProvider($controllerProvider);
	        routeConfig.setStateProvider($stateProvider);
	        /**设置 视图根路径**/
	        routeConfig.setBaseViewPath("core/media");
	        
	        /**论坛*/
//	        routeConfig.state({stateName: "index", 
//	        					views:{'body':{ templateUrl:'index.html',controller:"controllers/index"},
//									   'foot':{ templateUrl:'../copy.html',controller:null}
//	        					}, path:"bbs"}, []);
	        $stateProvider.state('index',{url:"/index",
				views:{'body':{ templateUrl:'core/media/bbs/index.html',controller:index.index},
					   'foot':{ templateUrl:'core/media/copy.html',controller:null}
			}});
	        routeConfig.state({stateName:"index", controller:"controllers/index", path:"bbs"},
					[{name:"register", controller: "controllers/column",templateUrl:"register.html"},
					 {name:"categorys", controller:"controllers/column", templateUrl: 'category_list.html'},
					 {name:"topicAll?cate_id&search&type", controller:"controllers/bbs/topic", templateUrl:"topic_list.html"},
					 {name:"addTopic", controller:"controllers/bbs/topic",templateUrl:"topic.html"},
					 {name:"topicInfo?topic_id", controller:"controllers/bbs/topic",templateUrl:"topic_info.html"}]);
	        /**在线工具**/
	        $stateProvider.state('tools',{url:"/tools",
					views:{'body':{ templateUrl:'core/media/tools/tools.html',controller:tools},
						   'foot':{ templateUrl:'core/media/copy.html',controller:null}
			}});
	        routeConfig.state({stateName:"tools", controller :"controllers/tools/tools"},
				    [{name:"code_runner", controller:"controllers/tools/code_runner",templateUrl:"code_runner.html"},   //如果 方法名 与 模板名字相同 则 可省略 templateUrl
				     {name:"sql_format", controller:"controllers/tools/sql_format"}]);
	        /**娱乐**/
	        $stateProvider.state('games',{url:"/games",
					views:{'body':{ templateUrl:'core/media/games/games.html',controller:games},
						   'foot':{ templateUrl:'core/media/copy.html',controller:null}
			}});
			routeConfig.state({stateName:"games", controller:"controllers/games/games", path:"games"},
				    [{name:"els_board", controller:"controllers/games/elsBoard", templateUrl:"elsBoard.html"}]);
	        
			/**个人空间**/
			$stateProvider.state('space',{url:'/space/:user_id',
	  			views:{'body':{templateUrl: 'core/media/space/space.html', controller:space},
	  				   'foot':{ templateUrl:'core/media/copy.html',controller:null}
	  		}});
			
			routeConfig.state({stateName:"space", controller:"controllers/space/space", path:"space"},
					[{name:"info_center?self", templateUrl: function(stateParams){
						 if(stateParams.self == 'true'){
							 return "core/media/space/info_center.html";
						 }else{
							 return "core/media/space/info_center1.html";
						 }
					    },controller:"controllers/space/message", mName:"messageList"},
					 {name:"profile", controller:"controllers/space/profile"},
					 {name:"message_list", controller:"controllers/space/message"},
					 {name:"blog?blog_id", controller:"controllers/space/blog", mName:"add"},
					 {name:"blog_info?blog_id", controller:"controllers/space/blog", mName:"info"},
					 {name:"blog_list?blog_cate_id", controller:"controllers/space/blog", mName:"list"},
					 {name:"blog_category", controller:"controllers/space/blog_category"}]);
			
			/**关于**/
			$stateProvider.state('about',{ url:'/about', 
				views:{'body':{templateUrl: 'core/media/about/index.html'},
	  				   'foot':{ templateUrl:'core/media/copy.html',controller:null}}});
			
	        $urlRouterProvider.otherwise('/index');
	        
		}]);
		// 处理返回后的数据 , 如果未登录 再次统一处理
		app.factory('anthInterceptor', function(checkUser){		
			return {
			      'response': function(response) {
			    	  var url = response.config.url;
			    	  var method = url.substring(url.lastIndexOf("/")+1);
			    	  var reg = /.*(add|del|edit|comment).*/;
			    	  if(reg.test(method) && response.data.type == "not_login"){
			    		  checkUser.logout();
			    	  }
			    	  return response;
			      }
			};
		});
		
	return app;
});

